// pages/userCollectMenu/userCollectMenu.js
import api from '../../utils/api.js'
import { config } from '../../utils/config'
const user_id = wx.getStorageSync("user_id")
const params = {
    page: 1,
    size: 6
}
const app = getApp()
import { HTTP } from '../../utils/http.js'
let http = new HTTP()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        height: app.globalData.height * 2 + 20,
        cookbook: [],
        headlinesList: [],
        none: false, // 是否没有收藏
        current: 0,
        imgUrl: config.imgUrl,
        show: false, // 是否展示可选择编辑
        haveOne: false, // 编辑时是否选中至少一个 
    },
    // 接口
    // 拿到用户收藏的资讯列表
    getUserInformationList(obj) {
        http.request({
            url: api.getUserZixunList,
            data: obj,
            success: res => {
                console.log(res.data)
                if (res.msg == "该用户没有收藏任何资讯") {
                    this.setData({
                        headlinesList: []
                    })
                } else {
                    var headlinesList = res.data
                    headlinesList.map(item => {
                        item.flag = false
                        item.information.create_time = item.information.create_time.split(" ")[0]
                    })
                    this.setData({
                        headlinesList: res.data
                    })
                }
            }
        })
    },
    // 取消收藏接口
    addDelCollectCookbook(obj) {
        http.request({
            url: api.addDelCollectCookbook,
            data: obj,
            success: res => {
                console.log(res)
                if (res.code == 200) {
                    wx.showToast({
                        title: '删除成功',
                        duration: 1500,
                    })
                }
            }
        })
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {},
    goDetail(e) {
        const id = e.currentTarget.dataset.cookbook_id;
        var url = "/pages/video/video?id=" + id
        if (this.data.current == 1) {
            url = "/pages/headlinesDetail/headlinesDetail?id=" + id
        }
        wx.navigateTo({
            url
        })
    },
    change(e) {
        this.setData({
            current: e.currentTarget.dataset.idx,
            show: false,
        })
        if (e.currentTarget.dataset.idx == 0) {
            if (this.data.cookbook.length != 0) {
                this.setData({
                    none: false
                })
            } else {
                this.setData({
                    none: true
                })
            }
        } else {
            if (this.data.headlinesList.length != 0) {
                this.setData({
                    none: false
                })
            } else {
                this.setData({
                    none: true
                })
            }
        }
    },
    // 点击编辑按钮
    compile() {
        this.setData({
            show: true
        })
    },
    // 点击取消按钮
    cancel() {
        var cookbook = this.data.cookbook
        cookbook.map(item => {
            item.flag = false
        })
        this.setData({
            show: false,
            cookbook
        })
    },
    // 点击选中或者取消
    select(e) {
        if (this.data.current == 0) { // 菜谱选中
            var cookbook = this.data.cookbook
            var item = cookbook.filter(item => {
                return item.cookbook_id == e.currentTarget.dataset.cookbook_id
            })[0]
            item.flag = !item.flag
            var haveOne = false
            cookbook.map(item => {
                if (item.flag) {
                    haveOne = true
                    return
                }
            })
            this.setData({
                cookbook,
                haveOne
            })
        } else { // 味歌头条选中
            var headlinesList = this.data.headlinesList
            var item = headlinesList.filter(item => {
                return item.information.id == e.currentTarget.dataset.cookbook_id
            })[0]
            item.flag = !item.flag
            var haveOne = false
            headlinesList.map(item => {
                if (item.flag) {
                    haveOne = true
                    return
                }
            })
            this.setData({
                headlinesList,
                haveOne
            })
        }

    },
    //用户点赞或取消点赞一篇资讯文章的接口
    addDelCollectInformation(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.addDelCollectInforM,
                data: obj,
                success: res => {
                    console.log(res)
                    if (res.code == 200) {
                        wx.showToast({
                            title: '删除成功',
                            duration: 1500,
                        })
                    }
                }
            })
        })
    },
    // 点击删除按钮
    del() {
        if (!this.data.haveOne) {
            wx.showToast({
                title: '请勾选需要删除的菜谱',
                icon: "none",
                duration: 1500,
            })
            return
        }
        if (this.data.current == 0) { // 菜谱收藏
            var that =this
            wx.showModal({
                content: '确定删除收藏内容吗?',
                confirmColor: '#EA5D56',
                success: function (e) {
                    if (e.confirm) {
                        // this.addDelCollectCookbook({ userId: wx.getStorageSync("user_id"), cookbookId: item.cookbook_id, statusID: 2 })
                        var cookbook = that.data.cookbook
                        cookbook.map(item => {
                            if (item.flag) {
                                that.addDelCollectCookbook({ userId: wx.getStorageSync("user_id"), cookbookId: item.cookbook_id, statusID: 2 })
                            }
                        })
                        cookbook = cookbook.filter(item => {
                            return !item.flag
                        })
                        var list = wx.getStorageSync("shoucang-list2") || [];
                        wx.setStorageSync("shoucang-list2", cookbook);
                        if (cookbook.length == 0) { // 删除之后没有
                            that.setData({
                                none: true
                            })
                        }
                        that.setData({
                            cookbook,
                            show: false,
                            haveOne: false,
                        })
                    } else if (res.cancel) {
                        wx.navigateBack({
                            complete: (res) => {},
                        })
                        }
                }
                })
           
        } else {
            var headlinesList = this.data.headlinesList
            headlinesList.map(item => {
                if (item.flag) {
                    this.addDelCollectInformation({ user_id: wx.getStorageSync("user_id"), information_id: item.information.id, status_id: 2 })
                }
            })
            headlinesList = headlinesList.filter(item => {
                return !item.flag
            })
            if (headlinesList.length == 0) { // 删除之后没有
                this.setData({
                    none: true
                })
            }
            this.setData({
                headlinesList,
                show: false,
                haveOne: false,
            })
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

      console.log('onPullDownRefresh');
  
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
      console.log('触底刷新');
      
  },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        http.request({
            url: api.getCollectCooks,
            method: "GET",
            data: params,
            success: (res) => {
                if (res.data.length != 0) {
                    const data = res.data;
                    data.map(item => {
                        item.flag = false
                    })
                    console.log('data', data)
                    this.setData({
                        cookbook: data
                    })
                } else {
                    this.setData({
                        none: true
                    })
                }
            },
            fail: (res) => {

            }
        })
        this.getUserInformationList({ id: user_id })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})